package com.gelsanalyzer.application;

import java.awt.*;
import java.beans.*;
import java.util.List;

import javax.swing.*;

import org.apache.log4j.*;

import com.gelsanalyzer.business.*;
import com.gelsanalyzer.mvc.controller.*;
import com.gelsanalyzer.mvc.model.*;
import com.gelsanalyzer.mvc.view.*;

public class CourseOptimizerController extends AbstractController {
    private static final Logger log = Logger
        .getLogger(CourseOptimizerController.class);

    public CourseOptimizerController(IView courseOptimizerView,
                                     AbstractModel courseOptimizerModel) {
        super(courseOptimizerView, courseOptimizerModel);
        log.info("about to inject model/view dependancies");
        
        log.info("done constructing");
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        log.info("propertyChanged:" + evt);
        if ( evt.getPropertyName().equals(IController.APP_OPTIMIZED_COURSES) ) {
            List<IController> temp = parent.getChildren();
            for ( IController c : temp ) {
                if ( c instanceof CourseEntryController ) {
                    MappedList newVal = new MappedList(
                        (CourseEntryList) ((CourseEntryModel) c.getModel())
                            .getAbstractTableModel());
                    newVal.optimize();
                    ((CourseOptimizerView)view).setTextArea(newVal.toString());
                    setModelProperty(IController.APP_OPTIMIZED_COURSES, newVal);
                }
            }
            //
        }
    }

    @Override
    protected void setModelProperty(String propertyName, Object newValue) {
        log.info("setModelProperty(" + propertyName + "," + newValue + ")");
        super.setModelProperty(propertyName, newValue);
    }

}
